﻿import request from '@/utils/request'

// 获取主机信息
export function getHostInfo() {
  return request({
    url: '/iot/host-device/all-main-cabinet',
    method: 'get'
  })
}

// 获取健康评分数据
export function getHealthScoreData(deviceId) {
  return request({
    url: '/iot/host-device/health-score/latest',
    method: 'get',
    params: { deviceId }
  })
}

// 获取告警分析
export function getAlarmAnalysis(deviceKgId, scope) {
  return request({
    url: '/iot/host-device/alarm-records/summary',
    method: 'get',
    params: { deviceKgId, scope }
  })
}

// 获取历史健康评分数据
export function getHistoryHealthScore(deviceId, scope) {
  return request({
    url: '/iot/host-device/health-score/history',
    method: 'get',
    params: { deviceId, scope }
  })
}

function withRange(params, startTime, endTime) {
  if (startTime) {
    params.startTime = startTime
  }
  if (endTime) {
    params.endTime = endTime
  }
  return params
}

// 获取无功功率总量，支持自定义时间范围
export function getReactivePowerTotal(deviceKgId, scope, startTime, endTime) {
  return request({
    url: '/iot/host-device/reactive-power/total',
    method: 'get',
    params: withRange({ deviceKgId, scope }, startTime, endTime)
  })
}

// 获取有功功率总量，支持自定义时间范围
export function getActivePowerTotal(deviceKgId, scope, startTime, endTime) {
  return request({
    url: '/iot/host-device/active-power/total',
    method: 'get',
    params: withRange({ deviceKgId, scope }, startTime, endTime)
  })
}

// 获取运行时长总量，支持自定义时间范围
export function getRuntimeTotal(deviceKgId, scope, startTime, endTime) {
  return request({
    url: '/iot/host-device/runtime/total',
    method: 'get',
    params: withRange({ deviceKgId, scope }, startTime, endTime)
  })
}

// 获取用电量汇总，支持自定义时间范围
export function getActiveEnergySummary(deviceKgId, scope, startTime, endTime) {
  return request({
    url: '/iot/host-device/active-energy/summary',
    method: 'get',
    params: withRange({ deviceKgId, scope }, startTime, endTime)
  })
}

// 汇总全部主机（运行时长/功率/用电量）
export function getAggregateMetrics(scope, startTime, endTime) {
  return request({
    url: '/iot/host-device/metrics/aggregate',
    method: 'get',
    params: withRange({ scope }, startTime, endTime)
  })
}

// 查询全部主机指定范围内运行状态
export function getHostRunStatus(scope, startTime, endTime) {
  return request({
    url: '/iot/host-device/run-status',
    method: 'get',
    params: withRange({ scope }, startTime, endTime)
  })
}

// 分页查询主机运行报表
export function getHostOperationReport(params) {
  return request({
    url: '/iot/host-device/operation-report',
    method: 'get',
    params
  })
}

// 查询主机运行明细（开关柜电参 + 温度）
export function getDeviceSnapshots(params) {
  return request({
    url: '/iot/host-device/device-snapshots/page',
    method: 'get',
    params
  })
}

// 获取运行时长趋势
export function getRuntimeTrend(deviceKgId, scope) {
  return request({
    url: '/iot/host-device/runtime/trend',
    method: 'get',
    params: { deviceKgId, scope }
  })
}

// 故障记录列表
export function getFaultRecordList(deviceKgId, scope) {
  return request({
    url: '/iot/host-device/fault-records',
    method: 'get',
    params: { deviceKgId, scope }
  })
}
